/**
 * 百度Echarts统计通用配置项
 */
var _TextShape, _LineShape;
/**
 * 绑定数据到统计图上
 * @param {Object} c 配置项
 */
function initEcharts(c) {
	if (toString.apply(c) === '[object Object]') {
		if (!c.render) return false; //判断绑定对象是否存在值
		if (!c.type) return false; //判断图类型是否存在

		var _OPTIONS = {
			default: {},
			pie: {
				legend: {
					orient: 'horizontal',
					x: 'center',
					y: 'bottom',
					data: [],
					show: false
				},
				series: [{
					selectedMode: 'single',
					clickable: false,
					x: '0%',
					itemStyle: {
						normal: {
							label: {
								show: false
							},
							labelLine: {
								show: false
							}
						}
					},
					startAngle: 90,
					type: 'pie',
					radius: ['78%', '86%'],
					clickable: false
				}]
			},
			line: {
				xAxis: [{
					type: 'category',
					boundaryGap: false,
					data: []
				}],
				yAxis: [{
					type: 'value',
					axisLabel: {
						formatter: '{value}'
					}
				}],
				series: [{
					name: '最高气温',
					type: 'line',
					/*markPoint: {
						data: [{
							type: 'max',
							name: '最大值'
						}, {
							type: 'min',
							name: '最小值'
						}]
					},*/
					markLine: {
						data: [{
							type: 'average',
							name: '平均值'
						}]
					}
				}]
			},
			bar: {
				calculable: true,
				xAxis: [{
					type: 'category',
					splitLine: {
						show: false
					},
					data: []
				}],
				yAxis: [{
					type: 'value'
				}],
				grid:{
					 x:40,
					 y:10,
					 x2:10,
					 y2:30,
					 borderWidth:0
				},
				series: [{
					name: '生活费',
					type: 'bar',
					stack: '总量',
					itemStyle: {
						normal: {
							label: {
								show: true,
								position: 'inside'
							}
						}
					},
					data: []
				}]
			}
		};

		require.config({
			paths: {
				echarts: '../js/echarts/build/dist',
				theme:'../js/echarts/theme'
			}
		});
		require(['echarts', 'theme/macarons','echarts/chart/' + c.type], function(ec,defaultTheme) {
			var o = $.extend(true, _OPTIONS.default, _OPTIONS[c.type], c);
			var echarPie = ec.init($('#' + c.render)[0],defaultTheme);
			echarPie.setOption(o);

			//有文本需要绑定才进行文本绑定操作
			if (o.text) {
				_TextShape = _TextShape || require('zrender/shape/Text');
				_LineShape = _LineShape || require('zrender/shape/Line');
				var zr = echarPie.getZrender();
				setEchartsText(zr, {
					title: o.text
				}, {
					w: zr.getWidth() / 2,
					h: zr.getHeight() / 2,
					textColor: '#ffffff',
					textAlign: 'center',
					textFont: '"微软雅黑",Arial, Verdana, sans-serif'
				});
			}
		});
	}
}

function setEchartsText(zr, data, config) {
	//第一行文字
	zr.addShape(new _TextShape({
		style: {
			x: config.w,
			y: config.h,
			color: config.textColor,
			text: data.title,
			textAlign: config.textAlign,
			textFont: '14px ' + config.textFont
		},
		z: 10
	}));
}